'''
Created on 28/03/2014

@author: jpfuertes
'''
import unittest
import chordinstrument
import notes
import translator
from chordinstrument import Position, PositionWithOctaveOffset


class Test(unittest.TestCase):

    #
    #  Translator
    #
    def test_translator(self):
        translation = translator.Translator(chordinstrument.Guitarra(),chordinstrument.Charango())
        result = translation.translate_position(Position(0,1),moves=0)        
        self.assertEqual(result[0], PositionWithOctaveOffset(Position(2,1),0), "Unexpected translation result")
        
  
    #
    # Charango
    #
    def test_charango_3chord_is_lower_than_1chord(self):
        instrument = chordinstrument.Charango()        
        e_hight = instrument.get(Position(0,0))
        e_low   = instrument.get(Position(2,0))
        self.assertTrue(e_hight > e_low,"The first chord e should be higher than the e for the third.")
         
    def test_charango(self):
        instrument = chordinstrument.Charango()        
        music_note = instrument.get(Position(0,0))        
        self.assertTrue(music_note, notes.E(5))        
        music_note = instrument.get(Position(0,1))
        self.assertTrue(music_note, notes.F(5))        
        music_note = instrument.get(Position(0,3))
        self.assertTrue(music_note, notes.E(5))
        
    def test_find_notes_in_charango(self):
        instrument = chordinstrument.Charango()
        positions = instrument.find(notes.E(5))
        self.assertEqual(len(positions), 9, "E must be found just 9 times.")
        self.assertEqual(
            positions,[
            PositionWithOctaveOffset(Position(2,  0),-1),
            PositionWithOctaveOffset(Position(0,  0), 0), 
            PositionWithOctaveOffset(Position(1,  7), 0),
            PositionWithOctaveOffset(Position(2, 12), 0),
            PositionWithOctaveOffset(Position(3,  4), 0),
            PositionWithOctaveOffset(Position(4,  9), 0),
            PositionWithOctaveOffset(Position(0, 12), 1),
            PositionWithOctaveOffset(Position(1, 19), 1),
            PositionWithOctaveOffset(Position(3, 16), 1)
             ],
            "Expected positions doesn't match."
            )
    #
    # Guitarra
    #
    def test_guitarra(self):
        instrument = chordinstrument.Guitarra()        
        music_note = instrument.get(Position(0,12))        
        self.assertTrue(music_note, notes.E(4))        
        music_note = instrument.get(Position(1,12))
        self.assertTrue(music_note, notes.B(4))        
        music_note = instrument.get(Position(2,12))
        self.assertTrue(music_note, notes.G(4))
        music_note = instrument.get(Position(3,12))        
        self.assertTrue(music_note, notes.D(4))        
        music_note = instrument.get(Position(4,12))
        self.assertTrue(music_note, notes.A(3))        
        music_note = instrument.get(Position(5,12))
        self.assertTrue(music_note, notes.G(3))

    #
    # Chord
    #   
    def test_chord(self):
        istring = chordinstrument.IString(notes.E(5),chordinstrument.Charango.FRETS)        
        self.assertEqual(notes.G(5),istring.get(3),"Expected notes doesn't match")
        self.assertEqual(notes.A(5),istring.get(5),"Expected notes doesn't match")        
        self.assertEqual(notes.E(6),istring.get(12),"Expected notes doesn't match")        
    
    #
    # Note
    #    
    def test_note_prev(self):                
        self.assertEqual(notes.E(4), notes.F(4).prev(),"Expected notes doesn't match") 
        self.assertEqual(notes.B(3), notes.C(4).prev(),"Expected notes doesn't match")        
        
    def test_note_next(self):        
        self.assertEqual(notes.E(4).next(),notes.F(4),"Expected notes doesn't match") 
        self.assertEqual(notes.F(4).next(),notes.G(4).prev(),"Expected notes doesn't match")
        self.assertEqual(notes.G(4).next(),notes.A(4).prev(),"Expected notes doesn't match")
        self.assertEqual(notes.B(4).next(),notes.C(5),"Expected notes doesn't match")
        

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
                